package com.lucifer.treasure.network;

import net.fabricmc.api.ClientModInitializer;
import net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking;
import net.minecraft.client.MinecraftClient;

/**
 * 客户端网络包处理器 - 接收服务端发送的HUD数据
 */
public class SwordAuraHUDClientHandler {
    
    /**
     * 注册客户端网络包接收器
     */
    public static void register() {
        ClientPlayNetworking.registerGlobalReceiver(SwordAuraHUDPacket.ID, (packet, context) -> {
            // 确保在客户端主线程中执行
            context.client().execute(() -> {
                handleHUDPacket(packet);
            });
        });
        
        System.out.println("🌐 [网络包] 客户端HUD数据接收器已注册");
    }
    
    /**
     * 处理接收到的HUD数据包
     */
    private static void handleHUDPacket(SwordAuraHUDPacket packet) {
        try {
            // 调用客户端处理器更新HUD数据
            com.lucifer.treasure.SwordAuraClientHandler.updateHUDData(
                packet.isEnabled(),
                packet.getAuraLevel(),
                packet.getVisibleSwords(),
                packet.getHitEffectSwords(),
                packet.getRecoveringSwords(),
                packet.getMaxSwords(),
                packet.getHostilityCount()
            );
            
            // 调试输出（可选）
            // System.out.println("🌐 [网络包] 接收到HUD数据: 开启=" + packet.isEnabled() + 
            //                   " 等级=" + packet.getAuraLevel() + " 可见剑=" + packet.getVisibleSwords());
            
        } catch (Exception e) {
            System.err.println("❌ [网络包] 处理HUD数据失败: " + e.getMessage());
        }
    }
} 